package com.spaceobj.chess.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spaceobj.chess.common.Result;
import com.spaceobj.chess.entity.Move;
import com.spaceobj.chess.service.MoveService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "移动记录管理", description = "移动记录相关接口")
@RestController
@RequestMapping("/api/moves")
@RequiredArgsConstructor
public class MoveController {

    private final MoveService moveService;

    @Operation(summary = "记录移动")
    @PostMapping
    public Result<Move> recordMove(@RequestBody Move move) {
        moveService.save(move);
        return Result.success(move);
    }

    @Operation(summary = "更新移动记录")
    @PutMapping("/{id}")
    public Result<Move> updateMove(
            @Parameter(description = "移动ID") @PathVariable Integer id,
            @RequestBody Move move) {
        move.setMoveId(id);
        moveService.updateById(move);
        return Result.success(move);
    }

    @Operation(summary = "删除移动记录")
    @DeleteMapping("/{id}")
    public Result<Void> deleteMove(@Parameter(description = "移动ID") @PathVariable Integer id) {
        moveService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "获取移动记录详情")
    @GetMapping("/{id}")
    public Result<Move> getMove(@Parameter(description = "移动ID") @PathVariable Integer id) {
        return Result.success(moveService.getById(id));
    }

    @Operation(summary = "分页查询移动记录")
    @GetMapping
    public Result<Page<Move>> listMoves(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "游戏ID") @RequestParam(required = false) Integer gameId) {
        Page<Move> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Move> queryWrapper = new LambdaQueryWrapper<>();
        if (gameId != null) {
            queryWrapper.eq(Move::getGameId, gameId);
        }
        return Result.success(moveService.page(pageParam, queryWrapper));
    }
} 